import { FactoryTaskHelper } from "GameLoop/WorldState/EntityManager/Entitys/room/factory/FactoryTaskHelper";
import { State } from "framework/FSM/AState";
 

export class  FactoryGetResource<T extends IStateArg<Room>> extends State<T>
{

    public getActions(arg:T):Action[]
    {
        // global.Log.Info(`State_FactoryGetResource   assert1`);
        const actions:Action[] = [];
        const task = arg.entity.getTaskByType("factory")[0] ;
        
        const coreEntity = arg.entity.getEntity("CoreEntity");
       
        if( !coreEntity || !coreEntity.Factory)
        {
            return actions;
        }
        const curtask = task.data;
        if(curtask.arg.transportKey)
        {
            return []
        }
        const toptask = FactoryTaskHelper.Top(curtask.arg);
        if(toptask)
        {
            // 当前的生产不是最终生产，则所有的中间产物都可以从仓库取
            // 工厂空间不足。则把所需要得之外得资源全部清空
            const subResources = COMMODITIES[toptask.target].components;
           
            for (const resType in subResources)
            {
                // 判断每一种资源是否在Factory中满足要求。不满足的发送转移任务
                 const currestype = resType as FactoryResourceType;
                const needAmount = FactoryTaskHelper.ClacSubResourceAmount(toptask.target, toptask.amount-toptask.number,currestype)
                const storecount = coreEntity.Factory.store.getUsedCapacity( currestype);
                if(coreEntity.makeFreeSpace(currestype,needAmount,subResources))
                {
                    
                    return []
                }
                if(storecount < needAmount)
                {

                    // 资源不足
                    const code =  coreEntity.transferToFactoryTask({resourceType:currestype,amount:needAmount - storecount});
                    arg.entity.log("info",code ,"中央搬运任务数量",arg.entity.getTaskByType("core").length,'缺少资源',`[${currestype}:${storecount}]<[${needAmount}]`);
                    if(code === -1)
                    {
                        // 是复合资源。需要生产合成
                        curtask.arg.ProduceList.push({target:currestype as CommodityConstant,amount:needAmount  ,number:storecount});

                    }
                    else
                    {
                        if(typeof code =="string")
                        {
                            curtask.arg.transportKey = code;
                            arg.entity.log("info","更新运输任务ID" ,curtask.arg.transportKey);
                            return []
                        }
                        if(code ==1)
                        {
                            task.pause = 1000;
                            return []
                        }
                        // 当code ==1时，工厂需要进入休眠状态.然后检测资源变动后再唤醒
                       // global.Log.Info(`${arg.entity.MountRoom} State_FactoryGetResource  资源不足转入资源  assert1 ${JSON.stringify({ResourceType:currestype,Amount:needAmount - storecount})}`);
                    }

                }
                else
                {
                    // 资源足够不用管
                    //global.Log.Info(`${arg.entity.MountRoom} State_FactoryGetResource  资源足够 assert2 ${JSON.stringify({ResourceType:currestype,Amount:needAmount})}`);
                    coreEntity.log("info","工厂生产资源充足", JSON.stringify({ResourceType:currestype,Amount:needAmount}))
                }

            }

        }
        return actions;
    }
}
